import request from '@/utils/request'
const API_PREFIX = '/service-system/api/v1/role'

import { Role } from './types.ts'
import { Page, Option } from '@/api/types.ts'

/**
 * 角色分页列表
 * @param params
 * @returns
 */
export const getRoleList = (params: Role.Query) => {
    return request.get<Page<Role.Row>>(`${API_PREFIX}/page`, { params })
}

/**
 * 获取角色下拉列表
 */
export const getRoleOptions = () => {
    return request.get<Array<Option>>(`${API_PREFIX}/options`)
}

/**
 *  新增角色
 * @param params
 * @returns
 */
export const saveRole = (params: Role.Form) => {
    return request.post(`${API_PREFIX}`, params)
}

/**
 *  获取角色表单详情
 * @param roleId
 * @returns
 */
export const getRoleDetail = (roleId: number) => {
    return request.get<Role.Form>(`${API_PREFIX}/${roleId}/form`)
}
/**
 * 修改角色
 * @param params
 * @returns
 */
export const updateRole = (params: Role.Form) => {
    return request.put(`${API_PREFIX}/${params.id}`, params)
}

/**
 * 删除角色
 * @param ids 角色ID，多个以英文逗号分割
 * @returns
 */
export const deleteRole = (ids: string) => {
    return request.delete(`${API_PREFIX}/${ids}`)
}

/**
 * 修改角色状态
 * @param params { roleId: '角色ID', status: '状态 1：启用、0：禁用'}
 * @returns
 */
export const updateRoleStatus = (params: Role.Status) => {
    return request.patch(`${API_PREFIX}/${params.roleId}`, params)
}

/**
 *  获取角色的菜单ID集合
 * @param roleId 角色ID
 * @returns
 */
export const getRoleMenuIds = (roleId: number) => {
    return request.get<Array<number>>(`${API_PREFIX}/${roleId}/menuIds`)
}

/**
 * 分配菜单权限给角色
 * @param params { roleId: '角色ID', menuIds: '菜单ID集合'}
 * @returns
 */
export const assignMenuToRole = (params: Role.Assign) => {
    return request.put(`${API_PREFIX}/${params.roleId}`, params)
}
